package mysql.ddl.queries;

import design_patterns.command.CommandQueue;
import mysql.Connection;
import mysql.Schema;
import mysql.Table;
import mysql.commands.SqlCommand;

import java.sql.ResultSet;
import java.sql.SQLException;

import static mysql.ddl.SqlStrategy.LOAD_FILE;

/**
 * Created by nuno-rafael-figueiredo on 01-07-2014.
 */
public class SelectTables extends SqlCommand {
    private final CommandQueue queue = new CommandQueue();
    private Schema schema;

    public SelectTables(Connection connection, Schema schema) {
        super(connection, LOAD_FILE);
        this.schema = schema;
    }

    @Override
    public void execute() {
        try {
            ResultSet resultSet = connection.executeQuery(getSql());

            while (resultSet.next())
                queue.add(new SelectColumns(connection, new Table(schema, resultSet.getString(1))));

            bind(queue);
            queue.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
